package com.kh.pajx.sop.dao.bal.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.kh.pajx.sop.base.BaseDaoImpl;
import com.kh.pajx.sop.dao.bal.IBalMainBillDao;
import com.kh.pajx.sop.domain.bal.BalMainBill;

@Repository
@SuppressWarnings("unchecked")
public class BalMainBillDaoImpl extends BaseDaoImpl<BalMainBill> implements IBalMainBillDao {
	
	@Override
	public List<BalMainBill> findListByState(int type, int state) throws Exception{
		return getSession().createQuery(//
				"FROM BalMainBill bal WHERE bal.balType=? AND bal.billState=?") //
				.setParameter(0, type) //
				.setParameter(1, state) //
				.list();
	}
	
	@Override
	public List<BalMainBill> findListByState(int state) throws Exception{
		return getSession().createQuery(//
				"FROM BalMainBill bal WHERE bal.billState=?") //
				.setParameter(0, state) //
				.list();
	}
	
	public Integer getMaxBillCode(String deptCode){
		Object obj = getSession().createQuery(//
				"SELECT MAX(bal.billCode) FROM BalMainBill bal WHERE bal.billCode like :deptCode ")
				.setParameter("deptCode", deptCode+"%")
				.uniqueResult();
		if(obj!=null){
			return Integer.parseInt(obj.toString());
		}else {
			return 0;
		}
	}
	
	/*@Override
	public List<BalMainBill> findMyApp(String userId) throws Exception{
		return getSession().createQuery(//
				"FROM BalMainBill bal WHERE bal.appUserId=? ORDER BY bal.billCode DESC") //
				.setParameter(0, userId)
				.list();
	}*/
	
	@Override
	public BalMainBill getByCode(String billCode) throws Exception {
		return (BalMainBill)getSession().createQuery(//
				"FROM BalMainBill bal WHERE bal.billCode=?") //
				.setParameter(0, billCode) //
				.uniqueResult(); //
	}
	
	@Override
	public List<BalMainBill> findByMakDate(String startDate, String endDate) throws Exception {
		return getSession().createQuery(//
				"FROM BalMainBill bal WHERE bal.makDate >= ? AND bal.makDate <= ? AND bal.billState=3 ORDER BY bal.makDate DESC")
				.setParameter(0, startDate+"000000")
				.setParameter(1, endDate+"000000")
				.list();
	}


}
